﻿/// <reference path="libs/jquery-2.0.2.js" />
var currentFigure;

var engine = (function () {
    var isLegalPosition = function (coords, currentRotation, x, y) {
        var result = true;

        for (var r = 0; r < coords[currentRotation].length; r++) { //4 times -> array
            for (var c = 0; c < coords[currentRotation][r].length; c++) { //rows
                if (coords[currentRotation][r][c] == 1) {
                    //if we have a full cell in the field. We find all full cells in the figure that are full with a figure

                    // Left border collision check
                    if (x + c * CellSize < 0) {
                        result = false;
                    }

                    // Right border collision check
                    if (x + c * CellSize >= FieldCols * CellSize) {
                        result = false;
                    }

                    // Bottom border collision check
                    if (y + r * CellSize >= FieldRows * CellSize) {
                        result = false;
                    }

                    // Other figure collision check
                    if (y < 23 * CellSize) {
                        var row = (y / CellSize) + r;
                        var col = (x / CellSize) + c;

                        if (typeof (wiseTetrisGameField._fieldArray[row]) != "undefined" && wiseTetrisGameField._fieldArray[row][col] == 1) {
                            result = false;
                        }
                    }
                }
            }
        }
        return result;
    }

    var render = function () {

        setInterval(function () {
            currentFigure._element.empty();
            $scoreRes.empty();
            score++;
            statistics.init();
            var figure = document.createDocumentFragment();
            var cell = $("<div></div>")
            .css({
                width: CellSize,
                height: CellSize,
                border: "1px solid black",
                position: "absolute"
            });

            for (var row = 0; row < 4; row++) {
                for (var col = 0; col < 4; col++) {
                    if (currentFigure._coords[currentFigure._currentRotation][row][col] == 1) {
                        cell.css({
                            top: currentFigure._y + row * CellSize,
                            left: currentFigure._x + col * CellSize,
                            backgroundColor: currentFigure.color,
                        }).clone().appendTo(figure);
                    }
                }

                currentFigure._element.append(figure);
            }

            if (engine.isLegalPosition(currentFigure._coords, currentFigure._currentRotation, currentFigure._x, currentFigure._y + CellSize)) {
                currentFigure._y += CellSize;
            }
            else {
                //the figure reached the bottom, save its position as set 1 in appopriate cells in the game field matrix
                for (var row = 0; row < 4; row++) {
                    for (var col = 0; col < 4; col++) {
                        if (currentFigure._that._coords[currentFigure._that._currentRotation][row][col] == 1) {
                            wiseTetrisGameField._fieldArray[(currentFigure._y / 20) + row][(currentFigure._x / 20) + col] = 1;
                        }
                    }
                }

                $("#figures").empty();
                nextFigField.render();
                //trigger new figure
                gameObjectFactory.getRandomFigure();
            }

            currentFigure._element.appendTo("#figures");
            wiseTetrisGameField.render();
        }, 500);
    };

    return {
        isLegalPosition: isLegalPosition,
        render: render
    }
})();